﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;
using System.Data;

namespace AutoIndustryFiles
{
    class schneiderScraper:FileLinksScraper2
    {
        public List<string> ProductUrls = new List<string> { };
        public schneiderScraper()
        {
            this.MainPage = "http://www.schneider-electric.com/products/cn/ch/";
            this.CompanyName = "施耐德电气";
            this.CompanyUrl = "http://www.schneider-electric.com/products/cn/ch/";
            this.ColumnName = "主页 -> 产品与服务";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }

        public schneiderDoc ReadSchneiderFile(string path)
        {
            schneiderDoc doc = JsonConvert.DeserializeObject<schneiderDoc>(File.ReadAllText(path));
            return doc;
        }

        public void DocsToExcel(ExcelWrapper.ExcelApp ea,string path)
        {
            List<string> ds = Directory.EnumerateDirectories("C:\\ProductsList").ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("目录名", Type.GetType("System.String"));
            dt.Columns.Add("文档标题", Type.GetType("System.String"));
            dt.Columns.Add("文档编号", Type.GetType("System.String"));
            dt.Columns.Add("日期", Type.GetType("System.String"));
            dt.Columns.Add("链接", Type.GetType("System.String"));

            for (int i = 1; i <= 530; i++)
            {
                var files = from file in Directory.EnumerateFiles(ds[i], "*.cfc", SearchOption.AllDirectories)
                            orderby file ascending
                            select new { FileName = file};

                foreach(var f in files)
                {
                    schneiderDoc doc = ReadSchneiderFile(f.FileName);
                    if (doc.Total > 0)
                    {
                        foreach (AutoIndustryFiles.Row row in doc.Rows)
                        {
                            HtmlDoc row_doc = new HtmlDoc();
                            row_doc.LoadHtml(Convert.ToString(row.Cell[4]));

                            HtmlAgilityPack.HtmlNode n1 = row_doc.DocumentNode.SelectSingleNode("a[1]/img");
                            HtmlAgilityPack.HtmlNode n2 = row_doc.DocumentNode.SelectSingleNode("a[2]");
                            dt.Rows.Add(new object[]{ new DirectoryInfo(ds[i]).Name,//(n1 == null) ? Path.GetDirectoryName(ds[i]) : n1.GetAttributeValue("alt",Path.GetDirectoryName(ds[i])),
                                Convert.ToString(row.Cell[0]),
                                Convert.ToString(row.Cell[1]),
                                Convert.ToString(row.Cell[2]),
                                (n2 == null) ? "" : FormatUrl(n2.GetAttributeValue("href",""),"http://www.schneider-electric.com/"),
                                });
                        }
                    }
                }                
            }
            ea.OpenFile(path);
            ea.SetRangeValues(dt, true, "A1");
            ea.SaveAs(path);
        }

        public void CatalogDocs(ExcelWrapper.ExcelApp ea,string path)
        {
            List<string> ds = Directory.EnumerateDirectories("C:\\ProductsList").ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("目录名", Type.GetType("System.String"));
            dt.Columns.Add("文档标题", Type.GetType("System.String"));
            dt.Columns.Add("文档编号", Type.GetType("System.String"));

            for (int i = 530; i < ds.Count; i++)
            {
                var files = from file in Directory.EnumerateFiles(ds[i], "*.cfc", SearchOption.AllDirectories)
                            orderby file ascending
                            select new { FileName = file };

                foreach (var f in files)
                {
                    schneiderDoc doc = ReadSchneiderFile(f.FileName);
                    if (doc.Total > 0)
                    {
                        foreach (AutoIndustryFiles.Row row in doc.Rows)
                        {
                            HtmlDoc row_doc = new HtmlDoc();
                            row_doc.LoadHtml(Convert.ToString(row.Cell[4]));

                            HtmlAgilityPack.HtmlNode n1 = row_doc.DocumentNode.SelectSingleNode("a[1]/img");
                            HtmlAgilityPack.HtmlNode n2 = row_doc.DocumentNode.SelectSingleNode("a[2]");
                            dt.Rows.Add(new object[]{ new DirectoryInfo(ds[i]).Name,//(n1 == null) ? Path.GetDirectoryName(ds[i]) : n1.GetAttributeValue("alt",Path.GetDirectoryName(ds[i])),
                                Convert.ToString(row.Cell[0]),
                                Convert.ToString(row.Cell[1])
                                });
                        }
                    }
                }
            }
            ea.OpenFile(path);
            ea.SetRangeValues(dt, true, "A1");
            ea.SaveAs(path);
        }

        public void CopyDocs()
        {
            string src = @"C:\Users\Administrator\Desktop\allsession\Dump-0513-14-11-35\www.schneider-electric.com\download\filters\";
            string fn = "ResultGrid[{0}].cfc";

            List<string> ds = Directory.EnumerateDirectories("C:\\ProductsList").ToList();

            int i = 0;
            for (i = 1; i <= 246; i++)
                File.Copy(src + string.Format(fn,i-1),ds[i-1] + "\\" + string.Format(fn,i-1));
            for (i = 248; i <= 300; i++)
                File.Copy(src + string.Format(fn, i - 2), ds[i - 1] + "\\" + string.Format(fn, i - 1));
            //302
            for (i = 304; i <= 428; i++)
                File.Copy(src + string.Format(fn, i - 4), ds[i - 1] + "\\" + string.Format(fn, i - 1));
            for (i = 430; i <= 530; i++)
                File.Copy(src + string.Format(fn, i - 5), ds[i - 1] + "\\" + string.Format(fn, i - 1));
        }

        public void GetNamesOfAllProducts()
        {
            string url = "http://www.schneider-electric.com/download/cn/ch/products/current/?showAsIframe=true";

            HtmlDoc doc = new HtmlDoc();
            doc.LoadHtml(WebFuncs.OpenUrlEx(url, "UTF-8"));

            int i = 1;
            string prefix = "";

            foreach(HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(@"//div[@id=""odl_productsList""]//a"))
            {
                prefix = i.ToString().PadLeft(3, '0') + "_";
                i++;
                string path = @"c:\ProductsList\" + prefix + node.InnerText.Trim().Replace("/", "").Replace(":", "");
                
                try
                {
                    if (!Directory.Exists(path)) 
                    {

                        Directory.CreateDirectory(path);
                        ProductUrls.Add(FormatUrl(node.GetAttributeValue("href", ""), "http://www.schneider-electric.com/"));
                    }
                }
                catch
                {
                    File.AppendAllText(@"c:\ProductsList\error.txt", path + Environment.NewLine);
                }
            }
        }

        public override void GenerateAllLinks(string param)
        {
            string url = "http://www.schneider-electric.com/products/cn/ch/";

            FileLink lnk = new FileLink();
            lnk.Id = "0";//"pscat"; //pscat是总类别
            lnk.Name = "产品";
            lnk.CategoryPath = "产品";
            lnk.IsFileLink = false;
            lnk.Url = url;
            FileLinks.Add(lnk);
            
            bool f = false;
            UpdateMsg("schneiderScraper", lnk.Url, ref f);
            Application.DoEvents();

            HtmlDoc doc = new HtmlDoc();
            doc.LoadHtml(WebFuncs.OpenUrlEx(lnk.Url, "UTF-8"));
            GenerateSubWebLinks(doc, ref lnk, @"//div[@class=""landingCategory""]/div[1]/img", null, ExtractLinkAttr);

            int lower = 1;
            int upper = FileLinks.Count - 1;
            for (int i = lower; i <= upper; i++)
            {
                doc = new HtmlDoc();
                UpdateMsg("schneiderScraper",FileLinks[i].Url, ref f);
                doc.LoadHtml(WebFuncs.OpenUrlEx(FileLinks[i].Url, "UTF-8"));
                FileLink lnk_tmp = FileLinks[i];
                GenerateSubWebLinks(doc, ref lnk_tmp, @"//div[@class=""categoryRange""]/div[1]", null, ExtractLinkAttr2);
            }

            StringBuilder str = new StringBuilder();

            foreach (FileLink lnk3 in FileLinks)
                str.Append(lnk3.Id + "\t" + lnk3.Url + "\t" + lnk3.Name + "\t" + lnk.IsFileLink.ToString() + "\t" + lnk3.CategoryPath + "\t" + Environment.NewLine);

            File.WriteAllText(@"D:\外包项目\自动化资料下载\Output\schneider\AllCategories.txt", str.ToString());
        }

        void ExtractLinkAttr(HtmlAgilityPack.HtmlNode node, ref FileLink lnk)
        {           
            //lnk.Id = id;
            lnk.Url = node.GetAttributeValue("onclick", "").Replace("location.href='", "").Replace("'", "");
            lnk.Name = node.GetAttributeValue("alt", "").Trim();
            lnk.IsFileLink = false;
        }

        void ExtractLinkAttr2(HtmlAgilityPack.HtmlNode node, ref FileLink lnk)
        {
            //lnk.Id = id;
            lnk.Url = node.SelectSingleNode("a").GetAttributeValue("href", "");
            lnk.Name = node.SelectSingleNode("a/img").GetAttributeValue("alt", "");
            lnk.IsFileLink = true;
        }
    }
}
